package com.jwdemo
{
	
	import com.jwdemo.InfMoveableObject;
	import com.jwdemo.Vector4;

	public class Camera implements InfMoveableObject
	{
		
		private var screenDim: Vector4 ;
		private var mapDim: Vector4 ;
		private var _follow:InfMoveableObject ;
		
		private var _x:Number ;
		private var _y:Number ;
		private var _z:Number ;
			
		private	var	_mapwidth:Number ;
		private	var	_mapheight:Number ;
		private	var	_centrex:Number ;
		private	var	_centrey:Number ;
		private	var	_interp:Number ;

		private	var	_leftx:Number ;
		private	var	_topy:Number ;

		
		private	var	_time:Number ;
		private	var	_minx:Number ;
		private	var	_maxx:Number ;
		private	var	_miny:Number ;
		private	var	_maxy:Number ;

		private var floor:Function = function(val:Number):Number { return val ; }
			
		public function Camera(screenDim:Vector4, mapDim:Vector4, follow:InfMoveableObject, interp:Number = 0.2)
		{
			_follow		=	follow ;
			_x			=	0 ;
			_y			=	0 ;
			_z			=	0 ;
			_mapwidth	=	mapDim.X();
			_mapheight	=	mapDim.Y();
			_centrex	=	screenDim.X()/2 ;
			_centrey	=	screenDim.Y()/2 ;
			_interp		=	interp ;
			_leftx		=	0 ;
			_topy		=	0 ;
			_time		=	0 ;
			Init() ;
			
		}
		
		
		private function Init():void
		{
			if (this._follow) 
			{
				var pos:Object	=	this._follow.getPosition() ;
				this._x 		=	int(pos._x) ;
				this._y			=	int(pos._y) ;
				this._z 		=	pos._z ;
			}
			
			this.setRestriction(this._centrex,this._mapwidth-this._centrex,this._centrey,this._mapheight-this._centrey) ;
//			this._vpos		=	new Vector4(this._x,this._y) ;
		}

		public function setRestriction(minx:Number,maxx:Number,miny:Number,maxy:Number): void
		{
			this._minx	=	minx ;
			this._maxx	=	maxx ;
			this._miny	=	miny ;
			this._maxy	=	maxy ;
		}
		

		public function isFollowing(who:InfMoveableObject):Boolean
		{
			return this._follow == who ;
			
		}
		
		public function isAlive():Boolean
		{
			return true;
		}
		
		public function getTopLeft():Object 
		{
			return {x: this._leftx, y: this._topy} ;	
		} 

		public function getPosition():Object
		{
			return { _x: this._x , _y: this._y, _z: this._z  };
		}

		
		public function getVectorPosition():Vector4
		{
			return null;
		}

		public function setFollow(follow:InfMoveableObject,snap:Boolean = true) : void
		{
			this._follow		=	follow ;
			if (snap) 
			{
				var pos:Object	=	follow.getPosition()  ;
				this._x	=	int(pos._x) ;
				this._y	=	int(pos._y) ;
			}
		}

		public function update(dt:Number):void
		{
			this._time = this._time + dt  
			
			if (this._follow) 
			{
				// allow Camera to update Orbitor - if a callback exists.	
				//if (this._follow.CameraUpdate)
				//{
				//	this._follow.CameraUpdate(dt) ;
				//}
				
				var pos:Object = this._follow.getPosition() ;
				
				var followx:Number = floor(pos._x),followy:Number = floor(pos._y), followz:Number  = pos._z ;
				
				this._x = this._x + floor((followx	-	this._x)*dt/this._interp) ;
				this._y = this._y + floor((followy	-	this._y)*dt/this._interp) ;
				
				
				this._x = (this._x > this._maxx) ? this._maxx : this._x ;
				this._x = (this._x < this._minx) ? this._minx : this._x ;
				
				this._y = (this._y > this._maxy) ? this._maxy : this._y ;
				this._y = (this._y < this._miny) ? this._miny : this._y ;
				
				
				this._leftx		=	Math.floor(this._x - this._centrex) ;
				this._topy		=	Math.floor(this._y - this._centrey) ;
				
			}


		}
	}
}